dojo.provide("dojoscratch.TextScratch");
dojo.require("dojoscratch.Scratch");

//inheritance based on: http://www.aa3sd.net/using_dojo/draw_rectangle.html
dojo.declare("dojoscratch.TextScratch", dojoscratch.Scratch, {
	_init :function() {
		//examples: http://www.dreamingpoint.com/vexagon/js/dojox/gfx/tests/test_text.html
		this._shape.setFont({family: "Times", size: "36pt", weight: "bold"});
	},
	_connectToEvents : function() {
		//send transformations to server
		var domShape = this._group.getEventSource();
		dojo.connect(domShape, "onmouseup", this, "_handleMouseUp");
		dojo.connect(domShape, "ondblclick", this, "_handleDblClick");
	},
	setScalable: function(enable) {
		//text is not scalable
		return;
	},
	_handleDblClick: function(event) {
		//TODO externalize? (howto?)
		//XXX duplicate with TextCommand?
		var value = prompt("Value?", this._shape.getShape().text);
		if (!value) {
			return;
		}

		var newShape = this._shape.getShape();
		newShape.text = value;
		//do not send, update through channel
//		this._shape.setShape(newShape);

		var size = prompt("Size?", this._shape.getFont().size);
		if (!size) {
			return;
		}

		var newFont = this._shape.getFont();
		newFont.size = size;
		//do not send, update through channel
//		this._shape.setFont(newFont);
		
		dojo.publish("textTransform", [this._scratchId, newShape, newFont]);
	},
	setSettings:function(shape, font) {
		this._shape.setShape(shape);
		this._shape.setFont(font);
	},
	getTransformedBoundingBox: function() {
		//XXX breaking DRY :( try to fix this... (Scalable.js # _realBoundingBox())
		
		var box = this.getBoundingBox();
		var realMatrix = this._shape._getRealMatrix();

		var position = dojox.gfx.matrix.multiplyPoint(realMatrix, box.x, box.y);

		var size = dojox.gfx.matrix.multiplyPoint(this._shape.getTransform(), box.width, box.height);
		var sizeOffset = dojox.gfx.matrix.multiplyPoint(this._shape.getTransform(), 0, 0);
		return {x: position.x, y: position.y, width: size.x-sizeOffset.x, height: size.y-sizeOffset.y};
	},	
	getBoundingBox: function() {
		var height = parseInt(this._shape.getFont().size)
		
		return {x: this._shape.getShape().x, y: this._shape.getShape().y - height,
				width: this._shape.getTextWidth()+5, height: height + 10}; 
	}
});